﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MpSoft.Library.DTO;
using MpSoft.Library.BUSS.Categories;

namespace MpSoft.Web.AdminCP.ToolsBar
{
    public partial class MultipleMenu : System.Web.UI.UserControl
    {
        #region Var 

        private string selectValue = string.Empty;

        #endregion

        #region Method

        private void BindControl()
        {
            lblMultiSelect.DataSource = MenuBUSS.Menu_List();
            lblMultiSelect.DataTextField = "MenuTitle";
            lblMultiSelect.DataValueField = "MenuCode";
            lblMultiSelect.DataBind();

            MenuSelect();
        }

        private string SelectValueMenu()
        {
            selectValue = string.Empty;
            foreach (ListItem item in lblMultiSelect.Items)
            {
                if (item.Selected)
                {
                    if (!string.IsNullOrEmpty(selectValue))
                        selectValue += "," + item.Value;
                    else
                        selectValue = item.Value;
                }
            }
            return selectValue;
        }

        private void MenuSelect()
        {
            if (!string.IsNullOrEmpty(selectValue))
            {
                string[] ArrMenu = selectValue.Split(',');
                foreach (var str in ArrMenu)
                    if (lblMultiSelect.Items.FindByValue(str) != null)
                        lblMultiSelect.Items.FindByValue(str).Selected = true;
            }
        }

        #endregion

        #region Event
       
        protected void Page_Load(object sender, EventArgs e)
        {
            BindControl();
        }

        #endregion

        #region Properties

        public string SelectMenu
        {
            set {
                selectValue = value;
            }
            get {
                return SelectValueMenu();
            }
        }

        #endregion
    }
}